package fun.sanguo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import fun.sanguo.model.dto.*;
import fun.sanguo.model.dto.record.RecordRoleInsert;
import fun.sanguo.model.dto.record.RecordRoleParam;
import fun.sanguo.model.dto.record.RecordRoleUpdate;
import fun.sanguo.model.entity.record.RecordRoleEntity;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.record.RecordRoleService;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 内容管理-角色列表 服务接口
 *
 * @author ZYP
 * @date 2023-04-25
 */
@RestController
public class RecordRoleController {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    protected Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    @Resource
    RecordRoleService recordRoleService;

    /**
     * 查询页面table列表
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/record/role/getPage")
    @ResponseBody
    public ResDto getPage(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info(reqDto.toString());
            // 验证账号等信息
            // TODO
            // 获取请求包体
            PageQuery<RecordRoleParam> recordRoleParamPageQuery = (PageQuery<RecordRoleParam>) reqDto.getData();
            // 业务处理
            PageInfo<RecordRoleEntity> recordRoleEntityPageInfo = recordRoleService.getPage(recordRoleParamPageQuery);
            // 封装响应
            resDto.setCode(ResEnums.SUCCESS.getCode());
            resDto.setMsg(ResEnums.SUCCESS.getMsg());
            resDto.setData(recordRoleEntityPageInfo);
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("查询角色记录列表过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("查询角色记录列表过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }

    /**
     * 页面新增
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/record/role/insert")
    @ResponseBody
    public ResDto insert(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info(reqDto.toString());
            // 验证账号等信息
            // TODO
            // 获取请求包体
            PageInsert<RecordRoleInsert> recordRoleInsertPageInsert = (PageInsert<RecordRoleInsert>) reqDto.getData();
            // 业务处理
            boolean result = recordRoleService.insert(recordRoleInsertPageInsert);
            // 封装响应
            if (result) {
                resDto.setCode(ResEnums.SUCCESS.getCode());
                resDto.setMsg(ResEnums.SUCCESS.getMsg());
            } else {
                resDto.setCode(ResEnums.FAILED.getCode());
                resDto.setMsg(ResEnums.FAILED.getMsg());
            }
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("新增角色记录过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("新增角色记录过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }

    /**
     * 页面编辑
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/record/role/update")
    @ResponseBody
    public ResDto update(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info(reqDto.toString());
            // 验证账号等信息
            // TODO
            // 获取请求包体
            PageUpdate<RecordRoleUpdate> recordRoleUpdatePageUpdate = (PageUpdate<RecordRoleUpdate>) reqDto.getData();
            // 业务处理
            boolean result = recordRoleService.update(recordRoleUpdatePageUpdate);
            // 封装响应
            if (result) {
                resDto.setCode(ResEnums.SUCCESS.getCode());
                resDto.setMsg(ResEnums.SUCCESS.getMsg());
            } else {
                resDto.setCode(ResEnums.FAILED.getCode());
                resDto.setMsg(ResEnums.FAILED.getMsg());
            }
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("编辑角色记录过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("编辑角色记录过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }

    /**
     * 页面删除
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/record/role/delete")
    @ResponseBody
    public ResDto delete(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info(reqDto.toString());
            // 验证账号等信息
            // TODO
            // 获取请求包体
            PageUpdate<RecordRoleUpdate> recordRoleUpdatePageUpdate = (PageUpdate<RecordRoleUpdate>) reqDto.getData();
            // 业务处理
            boolean result = recordRoleService.delete(recordRoleUpdatePageUpdate);
            // 封装响应
            if (result) {
                resDto.setCode(ResEnums.SUCCESS.getCode());
                resDto.setMsg(ResEnums.SUCCESS.getMsg());
            } else {
                resDto.setCode(ResEnums.FAILED.getCode());
                resDto.setMsg(ResEnums.FAILED.getMsg());
            }
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("删除角色记录过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("删除角色记录过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }
}
